﻿package com.wandian.view.components.ui
{
	import com.wandian.data.GameData;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class CardManage extends Sprite
	{
		private var _mcHuase:MovieClip;
		private var _mcCardNum:MovieClip;
		private var _mcCardNumBlack:MovieClip;
		private var cardCount:CardCount;
		private var huase:int;
		private var num:int;
		public var id:int;
		private var _mcCard:MovieClip;

		public function CardManage(mcCard:MovieClip, boolAdd:Boolean=true):void
		{
			this.cardCount=CardCount.instance();
			mcCard.mouseChildren=false;
			mcCard.cacheAsBitmap=true;
			this._mcHuase=GameData.getMovieClip("CardHuaSe");
			this._mcCardNum=GameData.getMovieClip("CardNum");
			this._mcCardNumBlack=GameData.getMovieClip("CardNumBlack");
			this._mcHuase.cacheAsBitmap=true;
			this._mcCardNum.cacheAsBitmap=true;
			this._mcCardNumBlack.cacheAsBitmap=true;
			this._mcHuase.mouseChildren=false;
			this._mcCardNum.mouseChildren=false;
			this._mcCardNumBlack.mouseChildren=false;
			this._mcCard=mcCard;
			if (boolAdd)
			{
				this.addChild(this._mcCard);
			}
			this._mcCard.gotoAndStop(1);
			return;
		}

		public function get card():MovieClip
		{
			return this._mcCard;
		}

		override public function set visible(boolVisible:Boolean):void
		{
			this._mcCard.visible=boolVisible;
			return;
		}

		public function removeCard():void
		{
			if (this._mcCard == null)
			{
				return;
			}
			if (this._mcHuase != null)
			{
			}
			if (this._mcCard.contains(this._mcHuase))
			{
				this._mcCard.removeChild(this._mcHuase);
			}
			if (this._mcCardNum != null)
			{
			}
			if (this._mcCard.contains(this._mcCardNum))
			{
				this._mcCard.removeChild(this._mcCardNum);
			}
			if (this._mcCardNumBlack != null)
			{
			}
			if (this._mcCard.contains(this._mcCardNumBlack))
			{
				this._mcCard.removeChild(this._mcCardNumBlack);
			}
			return;
		}

		public function showCard(numAndHuase:String, isPlay:Boolean=true):Array
		{
			var str:*=numAndHuase;
			this.removeCard();
			try
			{
				this.huase=this.cardCount.countCardHuaSe(str.substring(1, 2));
				this.num=this.cardCount.countCardNum(str.substring(0, 1));
				if (isPlay)
				{
					this._mcCard.addFrameScript((this._mcCard.totalFrames - 1), this.showFun);
					this._mcCard.gotoAndPlay(1);
				}
				else
				{
					this._mcCard.gotoAndStop(this._mcCard.totalFrames);
					this.showFun();
				}
			}
			catch (err:Error)
			{
			}
			return [this.huase, this.num];
		}

		private function showFun():void
		{
			if (this._mcCard == null)
			{
				return;
			}
			this._mcCard.stop();
			this.removeCard();
			if (this.huase == 1 || this.huase == 3)
			{
				this._mcCardNum.gotoAndStop(this.num);
				this._mcCard.addChild(this._mcCardNum);
			}

			else
			{
				this._mcCardNumBlack.gotoAndStop(this.num);
				this._mcCard.addChild(this._mcCardNumBlack);
			}
			this._mcHuase.gotoAndStop(this.huase);
			this._mcCard.addChild(this._mcHuase);
			return;
		}

		public function removeThis():void
		{
			if (this._mcCard == null)
			{
				return;
			}
			if (this._mcHuase != null)
			{
			}
			if (this._mcCard.contains(this._mcHuase))
			{
				this._mcCard.removeChild(this._mcHuase);
				this._mcHuase=null;
			}
			if (this._mcCardNum != null)
			{
			}
			if (this._mcCard.contains(this._mcCardNum))
			{
				this._mcCard.removeChild(this._mcCardNum);
				this._mcCardNum=null;
			}
			if (this._mcCardNumBlack != null)
			{
			}
			if (this._mcCard.contains(this._mcCardNumBlack))
			{
				this._mcCard.removeChild(this._mcCardNumBlack);
				this._mcCardNumBlack=null;
			}
			this._mcCard=null;
			return;
		}

	}
}
